<template>
  <el-dialog :title="!form.id ? '新增' : '修改'" :visible.sync="visible" :close-on-click-modal="false" width="70%" top="10vh">
    <el-form :model="form" label-width="120px" ref="dataForm">
      <el-row>
        <el-col :xs="24" :sm="12" :md="8">
          <el-form-item label="登录账号" prop="username" :rules="rules.isRange('1-20')">
            <el-input v-model="form.username" placeholder="请输入登录账号" maxlength="20" show-word-limit/>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" v-if="!form.id">
          <el-form-item label="登录密码" prop="password" :rules="rules.isRange('6-20')">
            <el-input v-model="form.password" placeholder="请输入登录密码" maxlength="20" show-word-limit/>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" v-else>
          <el-form-item label="登录密码" prop="password">
            <el-input v-model="form.password" placeholder="不修改登录密码可不填写" maxlength="20" show-word-limit/>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" v-if="form.kind != 1">
          <el-form-item label="状态" prop="status" :rules="rules.isNotNull">
            <el-radio-group v-model="form.status">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="0">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8">
          <el-form-item label="姓名" prop="name" :rules="rules.isRange('1-10')">
            <el-input v-model="form.name" placeholder="请输入姓名" maxlength="10" show-word-limit/>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8">
          <el-form-item label="手机号码" prop="mobile" :rules="rules.isPhone">
            <el-input v-model="form.mobile" placeholder="请输入手机号码" maxlength="11" show-word-limit/>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8">
          <el-form-item label="邮箱" prop="email" :rules="rules.isEmail">
            <el-input v-model="form.email" placeholder="请输入邮箱" maxlength="30" show-word-limit/>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8">
          <el-form-item label="头像" prop="icon" :rules="rules.isNotNull">
            <el-upload class="upload-demo" action="" :file-list="iconList"
                       :limit=1 list-type="picture" accept="image/*" v-model="form.icon"
                       :on-remove="(value) => fileRemove(value, 'icon', 'iconList')"
                       :http-request="(value) => fileUpload(value, 'icon', 'iconList')">
              <el-button size="small" type="primary">上传图片</el-button>
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" v-if="form.kind != 1">
          <el-form-item label="拥有角色">
            <el-checkbox-group v-model="roleIdsList">
              <el-checkbox v-for="label in roleList" :label="label.id" :key="label.id">{{label.roleName}}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button icon="el-icon-close" @click.native="visible = false" size="small">取消</el-button>
      <el-button icon="el-icon-check" type="primary" :loading="isDisabled" size="small"
                 @click.native="saveOrUpdateForm(form, '/sysUser/save', '/sysUser/update')">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        rules: this.getRules(),
        isDisabled: false,
        iconList: [],
        roleList: [],
        roleIdsList: [],
        form: {
          icon: '',
          username: '',
          password: '',
          name: '',
          salt: '',
          email: '',
          mobile: '',
          createUserId: 0,
          kind: 2,
          status: 1,
          flag: 1
        }
      }
    },
    created () {
      this.httpGet('/sysRole/select').then(res => {
        this.roleList = res.data
      })
    },
    computed: {
      userIcon: {
        get () { return this.$store.state.user.icon },
        set (val) { this.$store.commit('user/updateIcon', val) }
      },
      userId: {
        get () { return this.$store.state.user.id }
      }
    },
    methods: {
      init (id) {
        this.visible = true
        this.isDisabled = false
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
        this.form.id = id
        if (id) {
          this.httpGet(`/sysUser/info/${id}`).then(res => {
            this.form = res.data
            this.roleIdsList = this.form.roleIdList
            this.iconList = this.urlListHandle(this.form.icon)
          })
        } else {
          this.iconList = []
          this.roleIdsList = []
        }
      },
      saveOrUpdateForm (form, saveUrl, updateUrl) {
        form['roleIdList'] = this.roleIdsList
        this.saveOrUpdate(form, saveUrl, updateUrl, this.updateIcon)
      },
      updateIcon () {
        if (this.userId === this.form.id) {
          this.userIcon = this.form.icon
        }
      }
    }
  }
</script>

